package com.xdja.kointest.dsl.demo2;

import java.util.ArrayList;

/**
 * @author mashuaibing
 * @description 描述一下方法的作用
 * @date 2020/6/2
 */
public class mashuaibing {

    public static void main(String[] args) {
        System.out.println(ysf(3, 2));
    }

    /**
     * @param alls
     * @param indexKill
     */
    private static int ysf(int alls, int indexKill) {

        ArrayList<Integer> alive = new ArrayList<Integer>();
        ArrayList<Integer> alive2 = new ArrayList<Integer>();
        //开始计数
        int startIndex = 0;
        //第一轮 把活的放到alive2数组
        for (int i = 1; i <= alls; i++) {
            startIndex++;
            if (startIndex % indexKill != 0) {
                alive2.add(i);
            } else {
                startIndex = 0;
            }
        }
        //如果是一个数据 就可以返回
        if (alive2.size() == 1) {
            return alive2.get(0);
        }

        //继续剔除
        do {
            alive.clear();
            alive.addAll(alive2);
            alive2.clear();
            for (int i = 0; i < alive.size(); i++) {
                startIndex++;
                if (startIndex % indexKill != 0) {
                    alive2.add(alive.get(i));
                } else {
                    startIndex = 0;
                }
            }
        } while (alive2.size() > 1);
        return alive.get(0);
    }


}
